import { getSetting, putSetttingPoint } from "@/api/public";
import Cookies from "@/utils/cookies";

export default {
  namespaced: true,
  state: {
    isJump: 0,
    isNeedCode: 1,
    // 用户使用的平台 ，默认支付宝小程序
    miniApp: "alipay",
    appId: "",
    channelId: "",
    // 协议
    displayAgreements: [],
    // 跳转到其他app path
    path: "",
    tel: "",
    bdMail: "",
    // 控制模块显示
    isShow: 1,
    isLoading: true,
    // 首页图片跳转 isJump 为 1跳
    backgroundDTO: {
      imgeUrl: "",
      jump: 0,
    },
    // 价格
    totalPrice: "",
    isSubServiceOpen: 0,
  },
  getters: {
    // 生成订单时 需要传递小程序类型platformType
    getPlatformType(state) {
      const App = {
        alipay: 1,
      };
      return App[state.miniApp];
    },
  },
  mutations: {
    setIsJump(state, value) {
      state.isJump = value;
    },
    setIsNeedCode(state, value) {
      state.isNeedCode = value;
    },
    setMiniApp(state, value) {
      state.miniApp = value;
    },
    setAppID(state, value) {
      state.appId = value;
      Cookies.set("AppID", value);
    },
    setChannelId(state, value) {
      state.channelId = value;
    },
    setDisplayAgreements(state, value) {
      state.displayAgreements = value;
    },
    setPath(state, value) {
      state.path = value;
    },
    setTel(state, value) {
      state.tel = value;
    },
    setBdMail(state, value) {
      state.bdMail = value;
    },
    setIsShow(state, value) {
      state.isShow = value;
    },
    setIsLoading(state, value) {
      state.isLoading = value;
    },
    setBackgroundDTO(state, obj) {
      state.backgroundDTO = obj;
    },
    setTotalPrice(state, value) {
      state.totalPrice = value;
    },
    setIsSubServiceOpen(state, value) {
      state.isSubServiceOpen = value;
    },
  },
  actions: {
    //   获取所有公共资源
    getPublicSetting({ commit }) {
      // 控制跳转
      // getIsJump().then((res) => {});
      // // 控制协议
      // getSettingArgs().then((res) => {
      //   console.log("getSettingArgs", res);
      // });
      // // 控制显示验证码
      // getSettingNeedCode().then((res) => {
      //   commit("setIsNeedCode", res.data);
      // });
      getSetting().then((res) => {
        // commit("setAppID", res.data.appId);
        commit("setChannelId", res.data.channelId);
        commit("setDisplayAgreements", res.data.displayAgreements);
        commit("setIsNeedCode", res.data.isNeedCode);
        commit("setPath", res.data.path);
        commit("setTel", res.data.tel);
        commit("setBdMail", res.data.bdMail);
        // commit("setIsJump", 1);
        commit("setIsJump", res.data.isJump);
        // commit("setIsShow",0)
        commit("setIsShow", res.data.isShow);
        // 必须跟在setIsShow后面
        commit("setIsLoading", false);
        commit("setBackgroundDTO", res.data.backgroundDTO);

        commit("setTotalPrice", res.data.totalPrice);

        commit("setIsSubServiceOpen", res.data.isSubServiceOpen);
      });
    },
    getProvider({ commit }) {
      // 获取用户使用的小程序平台
      uni.getProvider({
        service: "oauth",
        success: function(res) {
          commit("setMiniApp", res.provider[0]);
        },
        fail: function(res) {
          // 获取失败目前情况多数是h5
          // self.$store.commit('user/setMiniApp','h5')
          commit("setMiniApp", "h5");
        },
      });
    },
    getAppId({ commit, state }) {
      if (state.miniApp === "alipay") {
        const appId = my.getAppIdSync();
        commit("setAppID", appId.appId);
      }
    },

    // sceneCode = M_TO_B_HONGSHENG
    // 发送埋点
    putPint({ rootState }) {
      putSetttingPoint({ sceneCode: "M_TO_B_HONGSHENG" });
    },
  },
};
